package org.java.guawa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BoundType;
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.MutableClassToInstanceMap;
import com.google.common.collect.SortedMultiset;
import com.google.common.collect.Table;
import com.google.common.collect.TreeMultiset;

/**
 * 锟斤拷锟斤拷Guavad锟侥伙拷锟斤拷锟斤拷锟斤拷汀锟�锟斤拷锟斤拷 锟斤拷锟斤拷 锟街凤拷 原锟斤拷
 * 
 * @author kongxp
 * 
 */
public class BaseGuava {

	public static void main(String[] args) {
		// str();
		collections();
	}

	private static void str() {
		// splitter
		Iterable<String> x = Splitter.on(',').trimResults().omitEmptyStrings()
				.split("foo,bar,,   qux");
		for (String string : x) {
			System.out.println(string);
		}
		// bytes
		byte[] bytes = "锟桔癸拷锟斤拷锟斤拷".getBytes(Charsets.UTF_8);

	}

	private static void collections() {
		// 统锟斤拷一锟斤拷锟绞碉拷锟斤拷锟斤拷太锟斤拷锟斤拷锟剿★拷
		Collection<String> words = new ArrayList<String>();
		for (int i = 0; i < 100; i++) {
			words.add("x" + (1000-('a' + i)));
		}
		Multiset<String> wordsMultiset = HashMultiset.create();
		wordsMultiset.addAll(words);
		for (String key : wordsMultiset.elementSet()) {
			// System.out.println(wordsMultiset.count(key));
			System.out.println(key);
		}
		// 锟斤拷锟斤拷锟脚ｏ拷疲锟斤拷锟斤拷园锟斤拷锟剿筹拷锟斤拷取锟斤拷锟较的讹拷
		TreeMultiset<Comparable<String>> sm = TreeMultiset.create();
		sm.addAll(words);
		for (Comparable<String> comparable : sm) {
			System.out.println(comparable);
		}
		SortedMultiset<Comparable<String>> re = sm.subMultiset("x100",
				BoundType.CLOSED, "x157", BoundType.CLOSED);
		System.out.println(re);

		//一锟斤拷key 锟斤拷锟斤拷锟叫革拷锟斤拷锟较碉拷value
		ListMultimap<String, Integer> multimap = ArrayListMultimap.create();
		for (int i = 0; i < 10; i++) {
			multimap.put("kong", 'd'+i);
		}
		for (String firstName : multimap.keySet()) {
			List<Integer> lastNames = multimap.get(firstName);
			System.out.println(firstName + ": " + lastNames);
		}

		//双锟斤拷map  锟斤拷锟斤拷要锟斤拷map锟叫碉拷value 也锟斤拷锟截革拷锟斤拷
		HashBiMap<String, String> bimap = HashBiMap.create();
		for (int i = 0; i < 10; i++) {
			bimap.put("x"+i, "y"+i);
		}
		System.out.println(bimap.inverse().get("y0"));
		
		
		
		//table  Table<R, C, V> as a Map<R, Map<C, V>> 一锟斤拷锟斤拷远锟斤拷应锟矫★拷
		Table<String, String, Double> weightedGraph = HashBasedTable.create();
		weightedGraph.put("v1", "c1", 4d);
		weightedGraph.put("v1", "c2", 4d);
		weightedGraph.put("v1", "c3", 4d);

		System.out.println(weightedGraph.row("v1")); // 
		System.out.println(weightedGraph.column("c1")); //
		
		
		///锟斤拷锟脚ｏ拷锟斤拷锟� 锟斤拷锟酵匡拷锟皆诧拷同锟斤拷锟斤拷  
		ClassToInstanceMap<Number> numberDefaults = MutableClassToInstanceMap.create();
		numberDefaults.putInstance(Integer.class, Integer.valueOf(0));
		numberDefaults.putInstance(Integer.class, Integer.valueOf(2));
		numberDefaults.putInstance(Double.class, Double.valueOf(2));
		//锟斤拷 也锟斤拷牛锟狡ｏ拷锟斤拷锟斤拷锟斤拷class锟斤拷key锟剿★拷
		System.out.println(numberDefaults.keySet());
		
	}

	private class Person {
		String firstName;
		String lastName;

	}
}
